package cn.xueden.system.controller;

import cn.xueden.common.result.Result;
import cn.xueden.system.model.entity.Alipay;
import cn.xueden.system.service.IAlipayService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**功能描述：支付宝支付接口
 * @author:梁志杰
 * @date:2025/6/10
 * @description:cn.xueden.system.controller
 * @version:1.0
 */
@RestController
@RequestMapping("/api/alipay")
@RequiredArgsConstructor
public class AlipayController {

    private final IAlipayService alipayService;

    /**
     * 分页查询
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/list")
    @PreAuthorize("hasAnyAuthority('role_admin')")
    public Result<IPage<Alipay>> list(@RequestParam(defaultValue = "1") Integer pageNo,
                                      @RequestParam(defaultValue = "10") Integer pageSize) {
        return Result.success( "获取成功",alipayService.getPageList(pageNo, pageSize));
    }

    /**
     * 新增
     * @param alipay
     * @return
     */
    @PostMapping
    public Result<Boolean> create(@RequestBody Alipay alipay) {
       boolean  result = alipayService.create(alipay);
        return result?Result.success(  "新增成功"):Result.failed("新增失败");
    }

    /**
     * 修改
     * @param alipay
     * @return
     */
    @PutMapping
    public Result<Boolean>update(@RequestBody Alipay alipay) {
        boolean  result = alipayService.updateAlipayById(alipay);
        return result?Result.success("修改成功"):Result.failed("修改失败");
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@PathVariable Integer id) {
        boolean  result = alipayService.deleteById(id);
        return result?Result.success("删除成功"):Result.failed("删除失败");
    }

    /**
     * 切换状态
     * @param id
     * @return
     */
    @PutMapping("/status/{id}")
    public Result<Boolean> switchStatus(@PathVariable Integer id) {
        try {
            boolean resultStatus = alipayService.switchStatusWithCheck(id);
            return Result.success("操作成功",resultStatus);
        } catch (RuntimeException e) {
            return Result.failed(e.getMessage());
        }
    }

}
